package com.test.down.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import java.lang.reflect.Method;

public class NetTools
{

    private static final class NetToolsInstance
    {
        private static final NetTools instance = new NetTools();
    }

    public static NetTools getInstance()
    {
        return NetToolsInstance.instance;
    }

    public final static int NONE = 0;

    public final static int ETHERNET = 1;

    // 无网络
    public final static int WIFI = 2;

    // Wi-Fi
    public final static int MOBILE = 3;

    public static final int NETWORK_CLASS_UNAVAILABLE = -1;

    public static final int NETWORK_CLASS_WIFI = -101;

    /**
     * Unknown network class.
     */
    public static final int NETWORK_CLASS_UNKNOWN = 0;

    /**
     * Class of broadly defined "2G" networks.
     */
    public static final int NETWORK_CLASS_2_G = 1;

    /**
     * Class of broadly defined "3G" networks.
     */
    public static final int NETWORK_CLASS_3_G = 2;

    /**
     * Class of broadly defined "4G" networks.
     */
    public static final int NETWORK_CLASS_4_G = 3;

    public static final int NETWORK_TYPE_WIFI = -101;

    public static final int NETWORK_TYPE_UNAVAILABLE = -1;

    /**
     * Network type is unknown
     */
    public static final int NETWORK_TYPE_UNKNOWN = 0;

    /**
     * Current network is GPRS
     */
    public static final int NETWORK_TYPE_GPRS = 1;

    /**
     * Current network is EDGE
     */
    public static final int NETWORK_TYPE_EDGE = 2;

    /**
     * Current network is UMTS
     */
    public static final int NETWORK_TYPE_UMTS = 3;

    /**
     * Current network is CDMA: Either IS95A or IS95B
     */
    public static final int NETWORK_TYPE_CDMA = 4;

    /**
     * Current network is EVDO revision 0
     */
    public static final int NETWORK_TYPE_EVDO_0 = 5;

    /**
     * Current network is EVDO revision A
     */
    public static final int NETWORK_TYPE_EVDO_A = 6;

    /**
     * Current network is 1xRTT
     */
    public static final int NETWORK_TYPE_1xRTT = 7;

    /**
     * Current network is HSDPA
     */
    public static final int NETWORK_TYPE_HSDPA = 8;

    /**
     * Current network is HSUPA
     */
    public static final int NETWORK_TYPE_HSUPA = 9;

    /**
     * Current network is HSPA
     */
    public static final int NETWORK_TYPE_HSPA = 10;

    /**
     * Current network is iDen
     */
    public static final int NETWORK_TYPE_IDEN = 11;

    /**
     * Current network is EVDO revision B
     */
    public static final int NETWORK_TYPE_EVDO_B = 12;

    /**
     * Current network is LTE
     */
    public static final int NETWORK_TYPE_LTE = 13;

    /**
     * Current network is eHRPD
     */
    public static final int NETWORK_TYPE_EHRPD = 14;

    /**
     * Current network is HSPA+
     */
    public static final int NETWORK_TYPE_HSPAP = 15;

    /**
     * * 获取当前网络状态
     *
     * @param context
     * @return
     */
    @SuppressLint("InlinedApi")
    public int getNetworkState(Context context)
    {
        try
        {
            if (null != context)
            {
                ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context
                        .CONNECTIVITY_SERVICE);
                // 手机网络判断
                NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)
                {
                    return MOBILE;
                }
                NetworkInfo ethernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
                if (null != ethernet)
                {
                    state = ethernet.getState();
                    if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)
                    {
                        return ETHERNET;
                    }
                }
                // Wifi网络判断
                state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)
                {
                    return WIFI;
                }
                return NONE;
            }
        } catch (Exception e)
        {
        }

        return -1;
    }

    /**
     * 获取网络类型
     *
     * @return
     */
    public static String getCurrentNetworkType(Context context)
    {
        int networkClass = getNetworkClass(context);
        String type = "unknown";
        switch (networkClass)
        {
            case NETWORK_CLASS_UNAVAILABLE:
                type = "nonet";
                break;
            case NETWORK_CLASS_WIFI:
                type = "wifi";
                break;
            case NETWORK_CLASS_2_G:
                type = "2g";
                break;
            case NETWORK_CLASS_3_G:
                type = "3g";
                break;
            case NETWORK_CLASS_4_G:
                type = "4g";
                break;
            case NETWORK_CLASS_UNKNOWN:
                type = "unknown";
                break;
        }
        return type;
    }

    public static int getNetworkClass(Context context)
    {
        int networkType = NETWORK_TYPE_UNKNOWN;
        try
        {
            final NetworkInfo network = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)
            ).getActiveNetworkInfo();
            if (network != null && network.isAvailable() && network.isConnected())
            {
                int type = network.getType();
                if (type == ConnectivityManager.TYPE_WIFI)
                {
                    networkType = NETWORK_TYPE_WIFI;
                } else if (type == ConnectivityManager.TYPE_MOBILE)
                {
                    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context
                            .TELEPHONY_SERVICE);
                    networkType = telephonyManager.getNetworkType();
                }
            } else
            {
                networkType = NETWORK_TYPE_UNAVAILABLE;
            }

        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return getNetworkClassByType(networkType);
    }

    public static int getNetworkClassByType(int networkType)
    {
        switch (networkType)
        {
            case NETWORK_TYPE_UNAVAILABLE:
                return NETWORK_CLASS_UNAVAILABLE;
            case NETWORK_TYPE_WIFI:
                return NETWORK_CLASS_WIFI;
            case NETWORK_TYPE_GPRS:
            case NETWORK_TYPE_EDGE:
            case NETWORK_TYPE_CDMA:
            case NETWORK_TYPE_1xRTT:
            case NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case NETWORK_TYPE_UMTS:
            case NETWORK_TYPE_EVDO_0:
            case NETWORK_TYPE_EVDO_A:
            case NETWORK_TYPE_HSDPA:
            case NETWORK_TYPE_HSUPA:
            case NETWORK_TYPE_HSPA:
            case NETWORK_TYPE_EVDO_B:
            case NETWORK_TYPE_EHRPD:
            case NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;
            case NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
        }
    }

    public boolean hasNet(Context context)
    {
        return NONE != getNetworkState(context);
    }

    /**
     * 判断网络是否可用
     *
     * @return
     */

    public boolean isNetworkAvailable(Activity activity)
    {
        Context context = activity.getApplicationContext();

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context
                .CONNECTIVITY_SERVICE);

        if (connectivityManager == null)
        {
            return false;
        } else
        {

            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0)
            {
                for (int i = 0; i < networkInfo.length; i++)
                {
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断gprs网络是否开启
     *
     * @param context
     * @return boolean
     */
    public boolean isGPRSActive(Context context)
    {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo.DetailedState detailedState = gprs.getDetailedState();
        return NetworkInfo.DetailedState.CONNECTED == detailedState;
    }

    /**
     * 判断wifi是否开启
     *
     * @param context
     * @return
     */
    public boolean isWifiActive(Context context)
    {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return wifi.isConnectedOrConnecting();
    }

    /**
     * 设置gprs状态(开启/关闭)
     *
     * @param context
     * @param flag
     * @return
     */
    public int setGPRSStatus(Context context, boolean flag)
    {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Method setMobileDataEnabl;
        try
        {
            setMobileDataEnabl = cm.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
            setMobileDataEnabl.invoke(cm, flag);
            return 0;
        } catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 设置wifi状态(开启/关闭)
     *
     * @param context
     * @param flag
     */
    public void setWifiStatus(Context context, boolean flag)
    {
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiManager.setWifiEnabled(flag);
    }

    /* * 获取网络类型
     *
     * @param context
     */
    public Object getNetType(Context context)
    {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connManager.getActiveNetworkInfo();
        // 无网络
        if (null == info)
        {
            return "unkonw";
        }
        // wifi
        if (info.getType() == ConnectivityManager.TYPE_WIFI)
        {
            return "wifi";
        }
        // 手机网络
        if (info.getType() == ConnectivityManager.TYPE_MOBILE)
        {
            return "3/4G";
        }
        return "error";
    }
}

